package cn.blue.web.controller;

import cn.blue.constant.RedisConstants;
import cn.blue.domain.House;
import cn.blue.result.JSONResult;
import cn.blue.result.PageList;
import cn.blue.service.IHouseService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    public IHouseService houseService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody House house){
        if(house.getHouseId()!=null){
            houseService.updateById(house);
        }else{
            houseService.insert(house);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        houseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(houseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(houseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody cn.blue.query.HouseQuery query){
        Page<House> page = new Page<House>(query.getPage(),query.getRows());
        page = houseService.selectPage(page);
        return JSONResult.success(new PageList<House>(page.getTotal(),page.getRecords()));
    }

    /**
     * 上架房源
     * @param houseId
     * @return
     */
    @RequestMapping(value="/online/{houseId}",method= RequestMethod.GET)
    public JSONResult online(@PathVariable("houseId") Long houseId ){

        return houseService.online(houseId);
    }
    /**
     * 展示房源
     * @param
     * @return
     */
    @RequestMapping(value="/showHouse",method= RequestMethod.GET)

    public JSONResult showHouse(){

        return JSONResult.success(houseService.showHouse());
    }
    /**
     * 下架房源
     *
     */
    @RequestMapping(value="/undercarriage/{houseId}",method= RequestMethod.GET)
    public JSONResult undercarriage(@PathVariable("houseId") Long houseId ){

        return houseService.undercarriage(houseId);
    }
    /**
     * 用户选择地点定位服务
     */
    @RequestMapping(value="/position",method= RequestMethod.GET)

    public JSONResult  position(){

        return JSONResult.success(houseService.position());
    }
    /**
     * 审核房源
     */
    @RequestMapping(value = "/examine",method = RequestMethod.POST)
    public JSONResult examineHouseSource(@RequestBody House house){
        Page<House> page = new Page<House>(query.getPage(),query.getRows());
        page = houseService.selectPage(page);
        return JSONResult.success(new PageList<House>(page.getTotal(),page.getRecords()));
    }





}
